package servlet;

import dao.Order_contentDao;
import dao.OrdersDao;
import dao.impl.Order_contentDaoImpl;
import dao.impl.OrdersDaoImpl;
import pojo.Dishes;
import pojo.Order_content;
import pojo.Orders;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@WebServlet(urlPatterns = "/orderServlet.do")
public class orderServlet extends HttpServlet {
    private OrdersDao ordersDao=new OrdersDaoImpl();
    private Order_contentDao order_contentDao=new Order_contentDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int flag_order=insertOrder(req);
        System.out.println("insert order:"+flag_order);
        int flag_order_content=insertOrder_Content(req);
        System.out.println("insert order_content:"+flag_order_content);
        req.getRequestDispatcher("finishOrder.html").forward(req,resp);
    }


    private int insertOrder(HttpServletRequest req){
        Orders order=new Orders();

        order.setOrd_no(null);
        order.setBus_no(Integer.valueOf( (String) getServletContext().getAttribute("bus_no")));
        order.setU_no((Integer) getServletContext().getAttribute("u_no"));
        order.setDel_no(null);
        order.setOrd_dtime(new Timestamp(System.currentTimeMillis()));
        order.setArr_dtime(null);
        order.setOrd_price(Float.valueOf(req.getParameter("sumAll")));
        order.setSt_time(null);
        order.setSt_del(false);
        order.setFi_del(false);

        System.out.println(order);
        return ordersDao.addOrder(order);
    }

    private int insertOrder_Content(HttpServletRequest req){
//        获得当前的订单号
        List<Orders> ordersList=ordersDao.queryOrder();
        Orders order=ordersList.get(ordersList.size()-1);
        Integer ord_no=order.getOrd_no();
//        获得菜品的数量
        List<Dishes> dishesList=(List<Dishes>)getServletContext().getAttribute("dishes");
        System.out.println("dishesList size:"+dishesList.size());
        Order_content order_content=new Order_content();
        Dishes dish;
        int count=0;
//        将每个菜品的数据写入order_content表中
        for (int i = 0; i < dishesList.size(); i++) {
            int num=Integer.valueOf(req.getParameter("order_num"+i));
//           没点到的栏目跳过就好
            if (num==0)
                continue;
            dish=dishesList.get(i);
            order_content.setCon_id(null);
            order_content.setOrd_no(ord_no);
            order_content.setDi_name(dish.getDi_name());
            order_content.setDi_num(num);
            order_content.setFi_price(Float.valueOf(req.getParameter("tol_price"+i)));
            System.out.println(order_content);
            count+=order_contentDao.addOrder_content(order_content);
        }
        return count;
    }
}
